/*
 * Created on Oct 15, 2003
 *
 */
package schoolsource.model;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.logging.Level;

import schoolsource.sql.SchoolDBSQL;
import schoolsource.util.SchoolLogger;

/**
 *
 * @author Steven Steele
 * Copyright 2003
 *
 * Contains information for homework submissions.
 */
public class SubmissionContainer extends Container {

    // The map of timer buckets for all databases
    protected static Hashtable dbStores = new Hashtable();

    private IntegerItem assignedClassID =
        new IntegerItem(SchoolDBSQL.CLASSASSIGNEDID, "", true);
    private IntegerItem assignedHomeworkID =
        new IntegerItem(SchoolDBSQL.HWASSIGNEDID, "", true);
    private IntegerItem assignedStudentID =
        new IntegerItem(SchoolDBSQL.USRASSIGNEDID, "", true);
    // The name of the file
    private StringItem fileName =
        new StringItem(SchoolDBSQL.SUBMISSIONFILENAME, "", true);
    // The path to the file
    private StringItem filePath =
        new StringItem(SchoolDBSQL.SUBMISSIONFILEPATH, "", true);
    // The size of the file
    private IntegerItem fileSize =
        new IntegerItem(SchoolDBSQL.SUBMISSIONFILESIZE, "0", true);
    private BooleanItem updateAllowed =
        new BooleanItem(SchoolDBSQL.SUBMISSIONUPDATEALLOWED, "FALSE", true);
    private DateItem submissionDate =
        new DateItem(SchoolDBSQL.SUBMISSIONDATE, "", true);


    /* (non-Javadoc)
     * @see schoolsource.model.Container#getValidationItems()
     */
    public Item[] getValidationItems() {
        ArrayList al = new ArrayList();

        al.add(assignedClassID);
        al.add(assignedHomeworkID);
        al.add(assignedStudentID);
        al.add(fileName);
        al.add(filePath);
        al.add(fileSize);
        al.add(updateAllowed);
        al.add(submissionDate);

        return (Item[]) al.toArray(new Item[al.size()]);
    }

    /**
     * Default constructor
     *
     */
    private SubmissionContainer() {

    }

    /**
     * Constructor that loads information from the database based on the ID passed in.
     * @param aID A natural number corresponding to an auto-generated attachment ID in the database.
     */
    private SubmissionContainer(String classID, String studentID, String homeworkID, String dbName) {
        getAssignedClassID().setValue(classID);
        getAssignedHomeworkID().setValue(homeworkID);
        getAssignedStudentID().setValue(studentID);
        SchoolDBSQL.getSchoolDBSQL(dbName).loadHomeworkSubmission(this);
    }

    public static SubmissionContainer getSubmissionContainer(String classID, String homeworkID, String studentID, String dbName) {
        SubmissionContainer returnVal = null;
        DBStore dbs = null;

        if (studentID.equals("-1") && homeworkID.equals("-1") && classID.equals("-1")) {
            returnVal = new SubmissionContainer();
        }
        else {
            if ((dbs = (DBStore) dbStores.get(dbName)) == null) {
                dbs = new DBStore();
                dbStores.put(dbName, dbs);
                dbs.start();
            }
            String key = "S" + studentID + "H" + homeworkID + "C" + classID;
            if ((returnVal = (SubmissionContainer) dbs.getContainer(key)) == null) {
                returnVal = new SubmissionContainer();
                returnVal.getAssignedHomeworkID().setValue(homeworkID);
                returnVal.getAssignedStudentID().setValue(studentID);
                returnVal.getAssignedClassID().setValue(classID);
                SchoolDBSQL.getSchoolDBSQL(dbName).loadHomeworkSubmission(returnVal);
                dbs.addContainer(returnVal, key);
            }
        }

        return returnVal;
    }

    public IntegerItem getAssignedClassID() {
        return assignedClassID;
    }

    public IntegerItem getAssignedStudentID() {
        return assignedStudentID;
    }

    public IntegerItem getAssignedHomeworkID() {
        return assignedHomeworkID;
    }

    public StringItem getFileName() {
        return fileName;
    }

    /**
     * Getter for the file path.
     * @return A string representation of the path where the attachment is stored
     * relative to doc.
     */
    public StringItem getFilePath() {
        return filePath;
    }

    /**
     * Getter for the file size.
     * @return A string representation of the file size, in bytes.
     */
    public IntegerItem getFileSize() {
        return fileSize;
    }

    public BooleanItem getUpdateAllowed() {
        return updateAllowed;
    }

    public DateItem getSubmissionDate() {
        return submissionDate;
    }

    /**
     * Prints the values of the container.
     */
    public void printItems() {
        SchoolLogger.logger.log(
            Level.INFO,
            "************************************************");
        Item iList[] = getValidationItems();
        for (int j = 0; j < iList.length; j++) {
            if (iList[j] != null) {
                SchoolLogger.logger.log(
                    Level.INFO,
                    iList[j].getFieldName() + ": " + iList[j].toString());
            }
        }
    }
}
